import QtQuick 2.5
//import com.syberos.basewidgets 2.0

import "../controls"
//import QtQml.Modules 2.0
import QtLocation 5.5
import QtPositioning 5.5
import QtQml.Models 2.2
import QtQuick.Controls 2.0
import "menucontent.js" as Content
Item
{
    id: menuTabView
    property bool tabVisible: false;
    signal selectProvider(string providerName)
    signal selectMapType(variant mapType)
    signal selectTool(string tool)

    property bool isFollowMe: false;
    property bool isMiniMap: false;

    property var providerlist:[]
    property var typelist:[]
    property var toollist:[]

    SwipeView
    {
        id: menuTabSwipeView
        anchors.fill: parent
        anchors.bottomMargin: 50

        function createProvider(plugins)
        {
            providerlist = []
            var index = 0;
            for (var i = 0; i < plugins.length; i++) {
                providerlist.push(plugins[i])
                console.log("map plugin contains " +  plugins[i] + "index is " , i)
                if (plugins[i].indexOf("osm") >= 0)
                    index = i
            }
            var array  = [index]
            console.log( array)
            //providerMenu
            providerMenu.select(index)
            menuTabView.selectProvider(plugins[index])
        }

    //    function toggleMiniMapState(minimap, parent)
    //    {
    //        if (minimap) {
    //            minimap.destroy()
    //            minimap = null
    //        } else {
    //            minimap = Qt.createQmlObject ('import "map"; MiniMap{ z: parent.z + 2 }', parent)
    //        }
    //    }

        function createTypeMenu(map)
        {
            typelist = []
            for (var i = 0; i<map.supportedMapTypes.length; i++)
            {
                typelist.push(map.supportedMapTypes[i])
            }
        }

        function createToolMenu(map)
        {
            toollist = []
            if (map.plugin.supportsGeocoding(Plugin.ReverseGeocodingFeature)) {
                toollist.push("RevGeocode")
                //addItem(qsTr("Reverse geocode")).triggered.connect(function(){selectTool("RevGeocode")})
            }
            if (map.plugin.supportsGeocoding()) {
                toollist.push("Geocode")
                //addItem(qsTr("Geocode")).triggered.connect(function(){selectTool("Geocode")})
            }
            if (map.plugin.supportsRouting()) {
                toollist.push("CoordinateRoute")
                toollist.push("AddressRoute")

                //addItem(qsTr("Route with coordinates")).triggered.connect(function(){selectTool("CoordinateRoute")})
                //addItem(qsTr("Route with address")).triggered.connect(function(){selectTool("AddressRoute")})
            }

            toollist.push("Language")
            toollist.push("MiniMap")
            toollist.push("FollowMe")


    //        var item = addItem("")
    //        item.text = Qt.binding(function() { return isMiniMap ? qsTr("Hide minimap") : qsTr("Minimap") })
    //        item.triggered.connect(function() {toggleMapState("MiniMap")})

    //        item = addItem("")
    //        item.text = Qt.binding(function() { return isFollowMe ? qsTr("Stop following") : qsTr("Follow me")})
    //        item.triggered.connect(function() {toggleMapState("FollowMe")})

            //addItem(qsTr("Language")).triggered.connect(function(){selectTool("Language")})
        }

        ListView
        {
            id: providerMenu
            function select(index)
            {
                if(view1.count > index )
                    view1.select(index, true)
            }
            //anchors.fill: parent
            model: menuTabView.tabVisible? menuTabView.providerlist:[]
            delegate:Button{
                id: delegate
                width: view1.width
                height: 80

                Rectangle
                {
                    anchors.fill: parent
                    anchors.baselineOffset: 10
                    border.width: 5
                    border.color: "black"

                }

                Text{
                    anchors.centerIn: parent; font.pixelSize: 40; color: "#67890a"
                    anchors.horizontalCenterOffset: -parent.width/4
                    text: modelData
                }
                onClicked:
                {
                    delegate.checked = true
                    //view1.select(index, true)
                    if (modelData.indexOf("osm") >= 0)
                        selectProvider(modelData)
                }
            }
        }


        ListView
        {
            id: typeMenu

            function select(index)
            {
                if(view2.count > index )
                    view2.select(index, true)
            }
            //anchors.fill: parent
            model: menuTabView.tabVisible? menuTabView.typelist:[]
            delegate:Button{
                id: delegate2
                width: view2.width
                height: 80

                Rectangle
                {
                    anchors.fill: parent
                    anchors.baselineOffset: 10
                    border.width: 5
                    border.color: "black"

                }

                Text{
                    anchors.centerIn: parent; font.pixelSize: 40; color: "#67890a"
                    anchors.horizontalCenterOffset: -parent.width/4
                    text: modelData.name
                }
                onClicked:
                {
                    delegate2.checked = true
                    //view2.select(index, true)
                    selectMapType(modelData)
                }

            }
        }

        ListView
        {
            id: toolMenu
            function select(index)
            {
                if(view3.count > index )
                    view3.select(index, true)
            }
            //anchors.fill: parent
            model: menuTabView.tabVisible? menuTabView.toollist:[]
            delegate:Button{
                id: delegate3
                width: view3.width
                height: 80

                Rectangle
                {
                    anchors.fill: parent
                    anchors.baselineOffset: 10
                    border.width: 5
                    border.color: "black"

                }

                Text{
                    anchors.centerIn: parent; font.pixelSize: 40; color: "#67890a"
                    anchors.horizontalCenterOffset: -parent.width/4
                    text: modelData
                }
                onClicked:
                {
                    delegate3.checked = true
                    //view3.select(index, true)
                    selectTool(modelData)
                }
            }
        }

    }

    PageIndicator
    {
        id: indicator

        count: menuTabSwipeView.count
        currentIndex: menuTabSwipeView.currentIndex

       // anchors.bottomMargin:  50
        anchors.top: menuTabSwipeView.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }

}




